﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PortalCurriculo.Util.CustomControls
{
    public class PopupButton
    {
        public PopupButton() { }

        public PopupButton(ButtonType type, string id, TextInfo textButton, ImageInfo imageButton, string cssClass, int order)
        {
            Type = type;
            ID=id;
            TextButton=textButton;
            ImageButton = imageButton; 
            CssClass = cssClass;
            Order = order;
        }

        #region Properties
        public ButtonType Type { get; set; }

        private string id;
        public string ID
        {
            get { return id; }
            set { if (string.IsNullOrEmpty(id)) id = value; }
        }

        private TextInfo textButton;
        public TextInfo TextButton
        {
            get { return textButton; }
            set { if (textButton == null) textButton = value; }
        }

        private ImageInfo imageButton;
        public ImageInfo ImageButton
        {
            get { return imageButton; }
            set { if (imageButton == null) imageButton = value; }
        }

        public bool CausesValidation { get; set; }

        private string cssClass;
        public string CssClass
        {
            get { return cssClass; }
            set { if (string.IsNullOrEmpty(cssClass)) cssClass = value; }
        }
        
        private int order;
        public int Order
        {
            get { return order; }
            set { if (order == 0) order = value; }
        }
        #endregion

        public Control BuildControl(string validationGroup)
        {
            switch (Type)
            {
                case ButtonType.Button:
                    var button = new Button
                                     {
                                         ID = ID,
                                         CssClass = CssClass,
                                         Text = TextButton.Text,
                                         CausesValidation = CausesValidation,
                                         ValidationGroup = validationGroup
                                     };
                    button.Click += TextButton.Click;
                    return button;
                case ButtonType.Image:
                    var imagebutton = new ImageButton
                                          {
                                              ID = ID,
                                              CssClass = CssClass,
                                              AlternateText = ImageButton.AlternateText,
                                              ImageUrl = ImageButton.ImageUrl,
                                              CausesValidation = CausesValidation,
                                              ValidationGroup = validationGroup
                                          };
                    imagebutton.Click += ImageButton.Click;
                    return imagebutton;
                default:
                    var linkButton = new LinkButton
                                         {
                                             ID = ID,
                                             CssClass = CssClass,
                                             Text = TextButton.Text,
                                             CausesValidation = CausesValidation,
                                             ValidationGroup = validationGroup
                                         };
                    linkButton.Click += TextButton.Click;
                    return linkButton;
            }
        }

        #region Nested type: ImageInfo

        public class ImageInfo
        {
            public ImageClickEventHandler Click { get; set; }

            private string alternateText;
            public string AlternateText
            {
                get { return alternateText; }
                set { if (alternateText == null) alternateText = value; }
            }

            private string imageUrl;
            public string ImageUrl
            {
                get { return imageUrl; }
                set { if (imageUrl == null) imageUrl = value; }
            }
        }

        #endregion

        #region Nested type: TextInfo

        public class TextInfo
        {
            public EventHandler Click { get; set; }
            public string Text { get; set; }
        }

        #endregion
    }
}